<?php
class Categoria_model extends CI_Model{
   private $table = 'tb_categoria';
    
    public function __construct() {
        parent::__construct();
    }
    
    function update($id, $user) {
        $this->db->where('cat_id', $id);
        $this->db->update($this->table, $user);
    }

    function delete($id) {
        $this->db->where('cat_id', $id);
        $this->db->delete($this->table);
    }

    function insert($user) {
        return $this->db->insert($this->table, $user);
    }

    function getAll() {
        $q = $this->db->query("select * from $this->table order by cat_descricao");
        $res = $q->result();
        return $res;
    }

    function getById($id) {
        $q = $this->db->where("cat_id = $id")->limit(1)->get($this->table)->row();
        return $q;
    }
    
    function getByDescription($descricao) {
        $q = $this->db->where("cat_descricao = '$descricao'")->limit(1)->get($this->table)->row();
        return $q;
    }
    
    function getAllFromDropDown(){
         foreach ($this->getAll() as $categoria) {
            $categories[$categoria->cat_id] = $categoria->cat_descricao;
        }
        return $categories;
    }
}

?>
